package com.study.activemq;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * Created by andys on 2017/12/2.
 */
public class JMSProducer {
    private static final int SEND_NUM = 10;

    public static void main(String[] args) {

        ConnectionFactory connectionFactory;//连接工厂
        Connection connection=null;
        Session session;
        Destination destination;//消息目的地
        MessageProducer messageProducer;

        connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD,ActiveMQConnection.DEFAULT_BROKER_URL);
        try {
            connection =connectionFactory.createConnection();
            connection.start();
            session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            destination = session.createQueue("First Queque1");

            messageProducer = session.createProducer(destination);
            sendMessage(session,messageProducer);
            session.commit();
        } catch (JMSException e) {
            e.printStackTrace();
        }finally{
            if (connection !=null){
                try {
                    connection.close();
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static void sendMessage(Session session,MessageProducer messageProducer) throws JMSException {
        for (int i = 0; i < JMSProducer.SEND_NUM; i++) {
                TextMessage message = session.createTextMessage("ActiveMQ 发布的消息"+i);
                System.out.println("发布消息"+"ActiveMQ发布的消息"+i);
                messageProducer.send(message);
        }
            
    }
}
